package com.myapp.wicket;
import java.util.*;
import dto.*;
import repository.*;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;
import org.apache.wicket.markup.html.panel.FeedbackPanel;

public final class AddNewProductPage extends WebPage implements AuthenticatedWebPage {

    public AddNewProductPage() {
        super();
        add(new FeedbackPanel("addingResult"));
        
        
        final TextField<String> pName = new TextField<String>("pName", new Model<String>());
        final TextField<String> pPrice = new TextField<String>("pPrice", new Model<String>());
        final TextField<String> pOnStorage = new TextField<String>("pOnStorage", new Model<String>());
        final TextField<String> pDescription = new TextField<String>("pDescription", new Model<String>());
        
        Form< ? > newProductForm = new Form<Void>("newProductForm") 
        {
        @Override
        protected void onSubmit()
        {
           boolean wasCreated=true;
           try
            {
             double price=Double.parseDouble(pPrice.getInput());
             int onStorage=Integer.parseInt(pOnStorage.getInput());
             wasCreated=(new ProductRepository()).Create(new Product(0,pName.getInput(),price, pDescription.getInput(),onStorage));
            }
            catch(Exception e)
            {
                wasCreated=false;
            }
           
          if (wasCreated)
          {
          info("New product was successfully created!!!");
          }
          else
          {
          info("Some error occured during creating new product...");
          }
        }
        };
        add(newProductForm);
         
        newProductForm.add(pName);
        newProductForm.add(pPrice);
        newProductForm.add(pOnStorage);
        newProductForm.add(pDescription);
        
    }
    
    public AddNewProductPage(PageParameters params) {
    }
}
